<%@ page import= "com.google.gdata.client.docs.*"%>
<%@ page import= "com.google.gdata.client.*"%>
<%@ page import= "com.google.gdata.data.docs.*"%>
<%@ page import= "com.google.gdata.data.acl.*"%>
<%@ page import= "com.google.gdata.util.*"%>
<%@ page import= "com.google.gdata.client.docs.DocsService" %>
<%@ page import= "com.google.gdata.data.BaseEntry"%>
<%@ page import= "com.google.gdata.util.AuthenticationException"%>
<%@ page import= "com.google.gdata.client.docs.DocsService"%>
<%@ page import= "com.google.gdata.util.ServiceException"%>
<%@ page import= "com.google.gdata.data.docs.DocumentListEntry"%>
<%@ page import= "com.google.gdata.data.docs.DocumentEntry"%>
<%@ page import= "com.google.gdata.data.docs.DocumentListFeed"%>
<%@ page import= "com.google.gdata.data.PlainTextConstruct"%>
<%@ page import= "com.google.gdata.client.DocumentQuery"%>
<%@ page import= "com.google.gdata.client.http.AuthSubUtil"%>
<%@ page import= "java.net.URL" %>
<%@ page import= "java.io.IOException"%>
<%@ page import= "java.net.MalformedURLException"%>
<%@ page import= "javax.servlet.http.*" %>
<%@ page import= "javax.servlet.http.HttpSession" %>
<%@ page import= "java.util.List" %>
<%@ page import= "com.google.gdata.data.DateTime"%>
<%@ page import= "com.google.gdata.data.Link"%>
<%@ page import= "javax.servlet.http.Cookie"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>SEDCloud</title>
        <link href="suporte/estilo01.css" rel="stylesheet" type="text/css" />
        <script language="JavaScript">
            <!--
            function extrairArquivo(){
                texto=document.form_upfile.arquivo.value;
                texto 	= texto.replace(/\\/g, "/");
                var Arquivo = texto.substring(texto.lastIndexOf('/') + 1);
                //var Extensao= texto.substring(texto.lastIndexOf('.') + 1);
                document.form_upfile.endereco.value=texto;
                //document.form_upfile.extencao_arquivo.value= Extensao ;
                document.form_upfile.titulo.value= Arquivo ;
                document.form_upfile.arquivo.value=texto;
                //return {arquivo:Arquivo, extensao:Extensao};
            }
            //-- >
        </script>
    </head>
    <body>
        <div id="menuhorizontal">
            <ul>
                <li><a href="<%=request.getContextPath() + "/index.jsp"%>">Inicio</a></li>
                <li><a href="#">Cursos</a></li>
                <li><a href="#">Agenda</a></li>
                <li id="current"><a href="<%=request.getContextPath() + "/lista_arquivo.jsp"%>">Arquivo</a></li>
                <li><a href="#">Aluno</a></li>
                <li><a href="#">Professores</a></li>
                <li><a href="#">Sair</a></li>
            </ul>
        </div>
        <div id="upfile">

            <form action="/GDataApi3/upload" method="post" name="form_upfile" id="form_upfile">
                <p>Nome do Arquivo:
                    <input type="file" name="arquivo" id="arquivo "onchange="extrairArquivo()" />                    
                    <input type="hidden" name="endereco" />
                    <input type="hidden" name="titulo" />
                    <input type="submit" name="Enviar" id="Enviar" value="Enviar" />
                </p>
            </form>
            
        </div>
        <div id="lista_arquivos">
            <%
            Cookie cookieToken = null;
            //String requestUrl = AuthSubUtil.getRequestUrl("http://localhost:8084/GDataApi3/gettoken", "http://docs.google.com/feeds/", false, true);
            DocumentListFeed feed = null;
            DocsService client = new DocsService("yourCo-yourAppName-v1");
            //le os Cookies do usuario verificando se ha token de segurança            
            String cookieName = "cookieToken";//nome do cookie
            //quando não existe cookies associados, o método getCookies() retorna um valor null
            Cookie listaPossiveisCookies[] = request.getCookies();//lista de cookie
            if (listaPossiveisCookies != null) {
                int numCookies = listaPossiveisCookies.length;
                for (int i = 0; i < numCookies; ++i) {
                    if (listaPossiveisCookies[i].getName().equals(cookieName)) { //procura pelo cookie
                        cookieToken = listaPossiveisCookies[i];
                        break;
                    }
                }
            }
            if (cookieToken != null) {
                client.setAuthSubToken(cookieToken.getValue(), null);
                client.setAuthSubToken(cookieToken.getValue(), null);
                URL feedUri = new URL("http://docs.google.com/feeds/default/private/full/");
                feed = client.getFeed(feedUri, DocumentListFeed.class);

            %>
            <table >
                <%
                for (DocumentListEntry entry : feed.getEntries()) {
                    String resourceId = entry.getResourceId();
                    String docType = entry.getType();
                    out.println("<tr><td>");
                    out.println(entry.getTitle().getPlainText() + " (" + docType + ")");
                    out.println("</td>");
                    out.println("<td>");
                    out.println("<a href=\"" + entry.getDocumentLink().getHref() + "\"target=\"_blank\">Abrir</a>");
                    out.println("</td>");
                    out.println("<td>");                    
                    out.println("<a href=\""+request.getContextPath() +"/delete?id=" + entry.getResourceId()+ "\">Excluir</a>");
                    out.println("</td></tr>");
                }
                %>
            </table>
            <%} else {
                //response.sendRedirect(requestUrl);
                out.println("tem que ta logado");
            }%>
        </div>
        <div id=token>
            <p><img src="http://code.google.com/appengine/images/appengine-silver-120x30.gif"
                    alt="Powered by Google App Engine" />
                <%
            if (cookieToken == null) {
                //response.sendRedirect(requestUrl);
            } else {
                out.print("Seu token: " + cookieToken.getValue());
            }%>
            </p>
        </div>
    </body>
</html>
